package cn.yougou.controller;

import cn.yougou.common.DtoUtil;
import cn.yougou.common.EmptyUtils;
import cn.yougou.common.MD5Util;
import cn.yougou.common.Responses;
import cn.yougou.config.LoginRequired;
import cn.yougou.dto.Dto;
import cn.yougou.pojo.YgUser;
import cn.yougou.service.user.YgUserService;
import cn.yougou.vo.YgPersonalVo;
import cn.yougou.vo.YgUserInfoVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * (YgUser)表控制层
 *
 * @author 白杰崇
 * @since 2020-12-03 01:32:27
 */
@RestController
@RequestMapping("ygUser")
@LoginRequired  //注意 ：用 @CurrentUser修饰的YgUserInfoVo是根据token自动从redis中读取并填充的!!!
public class YgUserController {
    /**
     * 服务对象
     */
    @Reference
    private YgUserService ygUserService;

    /**
     * 通过主键查询单条数据
     *
     * @param ygUser user pojo
     * @return 单条数据
     */
    @PostMapping("selectOne")
    public Dto selectOne(@RequestBody YgUser ygUser) {
       // System.out.println(ygUser.getId());
        return DtoUtil.returnDataSuccess(ygUserService.queryById(ygUser.getId()));
    }

    @PostMapping("test")
    public Dto selectOne() {
        // System.out.println(ygUser.getId());
        return DtoUtil.returnDataSuccess(new YgUser());
    }

    //注册
    @PostMapping("/success")
    public Dto success(@RequestBody Map<String,String>data) {
//        ygUserService.add(phone,s1);
//        return DtoUtil.returnDataSuccess(new YgUserInfoVo());
        String phone =data.get("phone");
        String password = data.get("password");
        String s1 = MD5Util.string2MD5(password);
        return  DtoUtil.returnDataSuccess(ygUserService.add(phone,s1));
    }
    
    /**
     * 根据地址 id 查询 详细信息
     */
    /*@GetMapping("login")
    public Responses info(@RequestBody YgUser ygUser) {
        YgUser yguser = ygUserService.login(ygUser);
        return EmptyUtils.isEmpty(yguser) ? Responses.error() : Responses.success(yguser);
    }*/

    //登录
    @PostMapping("login")
    public Dto login(@RequestBody YgUser ygUser) {
        String password = MD5Util.convertMD5(MD5Util.convertMD5(ygUser.getPassword()));
        ygUser.setPassword(password);
        YgUser yguser = ygUserService.login(ygUser);
        return DtoUtil.returnDataSuccess(ygUserService.queryById(yguser.getId()));
    }

    //查询个人信息
    @PostMapping("/selPersonal")
    public Dto selPersonal(@RequestBody YgPersonalVo ygPersonalVo){
        return DtoUtil.returnDataSuccess(ygUserService.selPersonal(ygPersonalVo));
    }
}